package com.neo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.CompositeRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator;
import org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Random;

/**
 * <Description> 手动刷新对外接口
 *
 * @author xianliru@100tal.com
 * @version 1.0
 * @createDate 2019/10/30 20:23
 */
@RestController
public class RefreshController {

//    @Autowired
//    private RefreshRouteService refreshRouteService;

    @Autowired
    ZuulHandlerMapping zuulHandlerMapping;

    @Autowired
    private CompositeRouteLocator compositeRouteLocator;
    @Autowired
    private DiscoveryClientRouteLocator discoveryClientRouteLocator;
    @Autowired
    private ZuulProperties zuulProperties;

    @GetMapping("/refreshRoute")
    public String refreshRoute() {
//        refreshRouteService.refreshRoute();
        return "refresh";
    }

    @RequestMapping("/watchNowRoute")
    public String watchNowRoute() {
        //可以用debug模式看里面具体是什么
        Map<String, Object> handlerMap = zuulHandlerMapping.getHandlerMap();
        return "watchNowRoute";
    }

    @GetMapping("/refreshRouteSimple")
    public String refreshRouteSimple() {
        Map<String, ZuulProperties.ZuulRoute> routes = zuulProperties.getRoutes();
        // 提取 routeList 数据并添加到routes中
        ZuulProperties.ZuulRoute route = new ZuulProperties.ZuulRoute();
        Integer i = new Random().nextInt(100);
        System.out.println("本次刷新：xf" + i);
        route.setPath("/xf" + i + "/**");
        route.setId("xf");
        route.setUrl("http://www.ityouknow.com");
        routes.put(route.getId(), route);
//        compositeRouteLocator.refresh();
//        discoveryClientRouteLocator.refresh();
        zuulHandlerMapping.setDirty(true);
        return "refresh";
    }

}
